package com.ziyucoding.springboot.labs.lab11.netty.demo1;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端
 *
 * @author ziyucoding
 * @date 2022-11-09
 **/
public class Server {

    private ServerSocket serverSocket;

    /**
     * 启动服务端
     *
     * @param port
     * @return null
     **/
    public Server(int port) {
        try {
            this.serverSocket = new ServerSocket(port);
            System.out.println("服务端启动成功，端口:" + port);
        } catch (IOException e) {
            System.out.println("服务端启动失败，端口:" + port);
            e.printStackTrace();
        }
    }

    public void start() {
        doStart();

        System.out.println("无法执行到我");
    }

    private void doStart() {
        while (true) {
            try {
                Socket socket = serverSocket.accept();
                new ClientHandler(socket).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
